package com.dwj.auto.comment.controller;

import com.dwj.auto.comment.model.vo.UserVo;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;

import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author: dangweijian
 * @description:
 * @create: 2020-07-07 14:36
 **/
public class BaseController {

    protected List<String> getAuthorities(){
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        Collection<? extends GrantedAuthority> authorities = authentication!=null?authentication.getAuthorities(): Collections.emptyList();
        return authorities.stream().map(GrantedAuthority::getAuthority).collect(Collectors.toList());
    }

    protected UserVo getCurrentUser(){
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        Object principal = authentication.getPrincipal();
        if(principal != null){
            return (UserVo) principal;
        }
        return null;
    }
}
